// logs.js
const util = require('../../utils/util.js')
const client=require('../../utils/client.js')

Page({
  data: {
    tabIndex:2,
    content:'',
    page:0,
    feedList:[
    //   {
    //   name:'王*五',
    //   avatar:'https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132',
    //   content:'扫码没有反应',
    //   createTime:'30分钟前',
    //   replyList:[{
    //     name:'流沙包',
    //     role:'user',
    //     content:'你说得不错，谢谢你的反馈，我们会努力改进的！'
    //   },{
    //     name:'流沙包',
    //     role:'admin',
    //     content:'你说得不错，谢谢你的反馈，我们会努力改进的！'
    //   }]
    // }
  
  ],
  },
  onLoad(options) {
    this.getList()
  },
  
  onShow(){
  
  },
  getList() {
    client.request('/app/wx/message/index','get',{page:this.data.page},(d)=>{
      let list=d.data;
      let oldList = this.data.feedList;
      this.setData({
        feedList:oldList.concat(list),
        page:this.data.page+1
      })
    });
  },
  clickTabItem: function(e) {
    console.log('clickItem',e.target.dataset.index)
    this.setData({
      tabIndex: e.target.dataset.index
    })
    console.log('tabIndex',this.data.tabIndex)
    if(this.data.tabIndex==2){
      this.setData({page:0,feedList:[]})
      this.getList()
    }
  },
  bindContent(e){
    this.setData({
      content:e.detail.value.replace(/\s*/g,"")
    })
  },
  toBottom: function(e) {
    console.log('滚动到底部',e)
    this.getList()
  },
  /**
   * 表单提交
   */
  saveData: function(e) {
    console.log('saveData_e',e)
    let _this = this;
    let content = e.detail.value.content
    let data={
     content
    }
    if(!this.validation(data)){
      wx.showModal({
        title:'友情提示',
        content: _this.data.error,
        showCancel: false
      });
      return false;
    }
    console.log('data',data)
    client.request('/app/wx/message/save','post',{message:content},(d)=>{
        if(d.code==0){
            wx.showToast({
              title: '提交成功',
              success(){
                _this.setData({tabIndex:2,content:''})
                _this.setData({page:0,feedList:[]})
                _this.getList()
              }
            });
        }
    });
    // client.request('/app/wx/inp','post',data,(d)=>{
    //     if(d.code==0){
    //       this.setData({isShowSubmit:true})
    //     }else{
    //       wx.showModal({
    //         content: d.msg,
    //         showCancel: false
    //       });
    //     }
    // })

  },

  /**
   * 表单验证
   */
  validation: function(values) {
    if (values.content == '') {
      this.data.error = '反馈内容不能为空';
      return false;
    }
    return true;
  }
})
